What does the R function `poly` really do?

Posted by merlin2011 on Stack Overflow See other posts from Stack Overflow or by merlin2011
Published on 2013-10-20T23:22:23Z Indexed on 2013/10/21 3:54 UTC
Read the original article Hit count: 127

Filed under:

I have read through the manual page ?poly (which I admit I did not completely comphrehend) and also read the description of the function in book Introduction to Statistical Learning.

My current understanding is that a call to poly(horsepower, 2) should be equivalent to writing horsepower + I(horsepower^2). However, this seems to be contradicted by the output of the following code.

library(ISLR)

summary(lm(mpg~poly(horsepower,2), data=Auto))$coef
summary(lm(mpg~horsepower+I(horsepower^2), data=Auto))$coef

Output:

                               Estimate Std. Error   t value      Pr(>|t|)
    (Intercept)            23.44592  0.2209163 106.13030 2.752212e-289
    poly(horsepower, 2)1 -120.13774  4.3739206 -27.46683  4.169400e-93
    poly(horsepower, 2)2   44.08953  4.3739206  10.08009  2.196340e-21
                        Estimate   Std. Error   t value      Pr(>|t|)
    (Intercept)     56.900099702 1.8004268063  31.60367 1.740911e-109
    horsepower      -0.466189630 0.0311246171 -14.97816  2.289429e-40
    I(horsepower^2)  0.001230536 0.0001220759  10.08009  2.196340e-21

My question is, why does the output not match, and what is poly really doing?

© Stack Overflow or respective owner

Related posts about r